package Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapNPCs
{
	import Classes.GameDatas.GD_MapGrid;
	import Classes.GameDatas.GameData;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapMoveTarget;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	
	/**
	 *地图NPC类 
	 * @author 葛增文
	 * 
	 */	
	public class MapNPC extends MapMoveTarget
	{
		public static const MAXWANDERDIS:int=5;
		public static const MAXWANDERWAITTIME:int=300;
		public static const CHANGEDIRPERCENT:int=20;
		
		private var _nextWanderCount:int;/*下一次漫游计数*/
		private var _wanderDis:int;/*漫游剩余距离*/
		private var _isWander:Boolean;/*是否处于漫游状态*/
		
		//private var _visionSP:Sprite;/*视野标记显示器*/
		public function MapNPC(gd:GameData):void
		{
			super(gd);
			this.init();
		}
		
		private function init():void
		{
			/*this._visionSP=new Sprite;
			var visionSPGraphics:Graphics=this.visionSP.graphics;
			visionSPGraphics.beginFill(0x0000ff);
			visionSPGraphics.drawCircle(0,0,10);
			visionSPGraphics.endFill();
			this.addChild(this.visionSP);
			this.visionSP.visible=false;*/
		}
		
		/**
		 *更新 
		 * 
		 */		
		override public function update():void
		{
			/*if(this.inVision)
			{
			this.visionSP.visible=true;
			}
			else
			{
			this.visionSP.visible=false;
			}*/
			if(this.isWander && !this.moveDir && !this.actionLocked)
			{
				if(this.nextWanderCount)
				{
					this.nextWanderCount-=1;
				}
				else
				{
					this.wanderDis=Math.random()*MAXWANDERDIS+1;
					this.beginWander();
				}
			}
			super.update();
		}
		
		/**
		 *开始漫游 
		 * 
		 */		
		private function beginWander():void
		{
			this.setState(10002003,false);
			
			var nextGrid:GD_MapGrid
			if(Math.random()*100>CHANGEDIRPERCENT)
			{
				nextGrid=this.getContinueMoveGrid(this.moveDir);
			}
			if(!nextGrid)
			{
				nextGrid=this.getRandomMoveGrid();
			}
			if(nextGrid)
			{
				this.beginMoveTo(nextGrid);
			}
			else
			{
				this.wanderDis=0;
				this.resetWanderWaitTime();
			}
		}
		
		/**
		 *下一步行为 
		 * 
		 */		
		override protected function nextStep():void
		{
			if(this.isWander)
			{
				if(this.wanderDis)
				{
					this.wanderDis-=1;
					this.beginWander();
				}
				else
				{
					this.resetWanderWaitTime();
				}
			}
		}
		
		/**
		 *重置漫游等待时间 
		 * 
		 */		
		private function resetWanderWaitTime():void
		{
			this.nextWanderCount=Math.random()*MAXWANDERWAITTIME+1;
		}
		
		/**
		 *获得继续移动的地图格 
		 * @param dir
		 * @return 
		 * 
		 */		
		protected function getContinueMoveGrid(dir:int):GD_MapGrid
		{
			if(!dir)return null;
			var targetGrid:GD_MapGrid=this.mapGrid.getNeighbourByDir(dir);
			if(targetGrid && targetGrid.conditionsCheck(this.moveConditionsAry))
			{
				return targetGrid;
			}
			return null;
		}
		
		/**
		 *获得下一步的随机地图格 
		 * @return 
		 * 
		 */		
		private function getRandomMoveGrid():GD_MapGrid
		{
			if(null==this.mapGrid)return null;
			var neighbourGridsAry:Array=GV_Map.instance.getNeighbourGridsAry(this.mapGrid);
			if(neighbourGridsAry==null)return null;
			for(var i:int=0;i<neighbourGridsAry.length;i++)
			{
				var checkGrid:GD_MapGrid=neighbourGridsAry[i];
				if(!checkGrid.conditionsCheck(this.moveConditionsAry))
				{
					neighbourGridsAry.splice(i,1);
					i--;
				}
			}
			if(neighbourGridsAry.length)
			{
				return neighbourGridsAry[int(Math.random()*neighbourGridsAry.length)] as GD_MapGrid
			}
			return null
		}
		
		/*getter & setter*/
		public function get nextWanderCount():int
		{
			return _nextWanderCount;
		}
		
		public function set nextWanderCount(value:int):void
		{
			_nextWanderCount = value;
		}
		
		override public function set mapGrid(value:GD_MapGrid):void
		{
			if(this.mapGrid)this.mapGrid.npc=null;
			super.mapGrid=value;
			if(this.mapGrid)this.mapGrid.npc=this;
		}
		
		public function get wanderDis():int
		{
			return _wanderDis;
		}
		
		public function set wanderDis(value:int):void
		{
			_wanderDis = value;
		}
		
		/**
		 * 是否处于漫游状态 
		 * @return 
		 * 
		 */		
		public function get isWander():Boolean
		{
			return _isWander;
		}
		
		public function set isWander(value:Boolean):void
		{
			_isWander = value;
			if(value)
			{
				this.resetWanderWaitTime();
			}
			else
			{
				this.nextWanderCount=0;
				this.wanderDis=0;
			}
		}
		
		/*public function get visionSP():Sprite
		{
		return _visionSP;
		}
		
		public function get inVision():Boolean
		{
		return this.mapGrid.inVision;
		}*/
		
	}
}